package cn.lijiguo.concurrency.part1.ch06;

/**
 * ThreadStopGraceful2
 *
 * @author lijiguo
 * @date 2019/12/13 14:44
 */
public class ThreadStopGraceful2 {

    private static class Worker extends Thread {

        @Override
        public void run() {
            while (true) {
                try {
                    Thread.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                    System.out.println("收到中断信号，线程即将停止。。。");
                    break;
                }
            }
        }
    }

    public static void main(String[] args) {
        Worker worker = new Worker();
        worker.start();

        try {
            Thread.sleep(2_000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        if (!worker.isInterrupted()) {
            worker.interrupt();
        }
    }
}
